/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.client;

import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.event.*;
import com.gwtext.client.data.*;    
import com.gwtext.client.widgets.grid.ColumnConfig;  
import com.gwtext.client.widgets.grid.ColumnModel;  
import com.gwtext.client.widgets.grid.GridPanel;  
import com.gwtext.client.core.*;
import java.util.*;

import nfse.gui.client.data.*;

public class DiskSpaceUsageGrid extends Panel
{

	private ArrayList dataList;
	private HeadsUpTab headsUpTab;
	private GridPanel grid;
	private ToolbarTextItem text;
	private boolean refresh = false;
	
	public DiskSpaceUsageGrid(int height, int width)
	{		
		this.setHeight(height);
		this.setWidth(width);
		this.setBorder(true);
	}

	public void buildGrid(ArrayList dataList, HeadsUpTab headsUpTab)
	{
		this.headsUpTab = headsUpTab;
		this.dataList = dataList;
		buildGrid();
	}
	
	public void buildGrid()
	{
		RecordDef recordDef = new RecordDef
		(
				new FieldDef[] 
				{
						new StringFieldDef("directory"),
						new StringFieldDef("total"),
						new StringFieldDef("used"),
						new StringFieldDef("available"),
						new StringFieldDef("percentUsed")
				}
		);
			
		grid = new GridPanel();	

		Object[][] data = convertRawDataToObjectGrid();
		MemoryProxy proxy = new MemoryProxy(data);
		
		ArrayReader reader = new ArrayReader(recordDef);
		Store store = new Store(proxy,reader);
		store.load();
		grid.setStore(store);
		
		ColumnConfig[] columns = new ColumnConfig[] {
				new ColumnConfig("Directory","directory",100),
				new ColumnConfig("Total","total",60)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				},
				new ColumnConfig("Used","used",100)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				},
				new ColumnConfig("Available","available",100)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				},				
				new ColumnConfig("Percent Used","percentUsed",100)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				}
				
				
		};
		
		 ColumnModel columnModel = new ColumnModel(columns);
		 
		grid.setColumnModel(columnModel);  
		grid.setFrame(true); 		 
		grid.setStripeRows(true);  
		grid.setAutoScroll(true);
		grid.setHeight(150);
		grid.setTitle("Disk Space Usage (1 K Blocks)");	
		
		Toolbar tBar = new Toolbar();
		ButtonListenerAdapter listener = new ButtonListenerAdapter() 
		{  
			public void onClick(Button button, com.gwtext.client.core.EventObject e) 
			{
				System.out.println("The Disk Space Usage refresh button was clicked");
				refreshThis();
				//updateFooterTimeStamp();
			}
		};
		
		ToolbarButton refreshBtn = new ToolbarButton("Refresh",listener);
		tBar.addButton(refreshBtn);
		grid.setBottomToolbar(tBar);
		
		 refresh = true;	
		this.add(grid);                                           						      
	}
	
	private void refreshThis()
	{
		this.headsUpTab.refreshDiskUsage();
	}
	
	
	public void refreshGrid(ArrayList dataList)
	{
		this.dataList = dataList;
		RecordDef recordDef = new RecordDef
		(
				new FieldDef[] 
				{
						new StringFieldDef("directory"),
						new StringFieldDef("total"),
						new StringFieldDef("used"),
						new StringFieldDef("available"),
						new StringFieldDef("percentUsed")
				}
		);
			
		Object[][] data = convertRawDataToObjectGrid();
		MemoryProxy proxy = new MemoryProxy(data);
		
		ArrayReader reader = new ArrayReader(recordDef);
		Store store = new Store(proxy,reader);
		store.load();
		
		ColumnConfig[] columns = new ColumnConfig[] {
				new ColumnConfig("Directory","directory",100),
				new ColumnConfig("Total","total",60)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				},
				new ColumnConfig("Used","used",100)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				},
				new ColumnConfig("Available","available",100)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				},				
				new ColumnConfig("Percent Used","percentUsed",100)
				{
					{
						setAlign(TextAlign.RIGHT);
					}
				}
				
				
		};
		
		 ColumnModel columnModel = new ColumnModel(columns);  
		 grid.reconfigure(store, columnModel);
	}
	
//	private void configureGridData(boolean refresh, GridPanel grid)
//	{
//		this.grid = grid;
//        this.refresh = refresh;
//        
//
//		 if (!refresh)
//		 {
//			 grid.setStore(store);
//			 grid.setColumnModel(columnModel);  			 
//		 }
//		 else
//		 {
//			 grid.reconfigure(store, columnModel);
//		 }
//		
//	}
	
		
	
	/**
	 * This converts the incoming data object a HashMap or an ArrayList into a Object[][]
	 */
	private Object[][] convertRawDataToObjectGrid()
	{
		int columns = 5;
		Object[][] displayData = null;
		
		if (dataList != null)
		{
			displayData = new Object[dataList.size()][columns];
			
			int recordCount = 0;
			
				Iterator it = dataList.iterator();
				while (it.hasNext())
				{
					DiskSpace diskSpace = (DiskSpace)it.next();
					displayData[recordCount][0] = diskSpace.getDataDirectory();
					displayData[recordCount][1] = diskSpace.getTotalDiskSpace();				
					displayData[recordCount][2] = diskSpace.getSpaceUsed();
					displayData[recordCount][3] = diskSpace.getSpaceAvailable();
					displayData[recordCount][4] = diskSpace.getPercentUsed();	
					recordCount++;
				}			
		
		}
		return displayData;
	}
	
	
	private Object[][] getTestData()
	{
		return new Object[][] {
		  		new Object[]{"/data","200","120","80","60"},
	    		new Object[]{"/data2","500","100","400","20"},
		  		new Object[]{"/data3","500","100","400","20"}
		};
	}
	
	public GridPanel getGrid()
	{
		return grid;
	}


	public void setGrid(GridPanel grid)
	{
		this.grid = grid;
	}
}
